"""
Case Type   : 事件触发器
Case Name   : 【事件触发器】CREATE/ALTER/DROP FOREIGN TABLE命令触发事件触发器
Create At   :2024/07/08
Owner       :sungang14
Description :
    预置条件：
        1.创建事件触发器：
       CREATE OR REPLACE FUNCTION f_eventtrigger_0025()
       RETURNS event_trigger
       LANGUAGE plpgsql
       AS $$ BEGIN
       RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;
       END;
       $$;
       CREATE EVENT TRIGGER start_eventtri ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0025();
       CREATE EVENT TRIGGER end_eventtri ON ddl_command_end EXECUTE PROCEDURE f_eventtrigger_0025();
       CREATE EVENT TRIGGER drop_eventtri ON sql_drop EXECUTE PROCEDURE f_eventtrigger_0025();
       CREATE EVENT TRIGGER rewrite_eventtri ON  table_rewrite EXECUTE PROCEDURE f_eventtrigger_0025(); 
    测试步骤：
        1、CREATE SERVER s_eventtrigger_0025 FOREIGN DATA WRAPPER file_fdw;
        2、CREATE FOREIGN  TABLE t_eventtrigger_0025(c1 integer NULL,c2 text NULL COLLATE pg_catalog."default")\
        SERVER s_eventtrigger_0025 OPTIONS (format 'text', filename '/home/$username/t_eventtrigger_0025.txt', delimiter ',');
        3、ALTER FOREIGN TABLE t_eventtrigger_0025 RENAME TO t_eventtrigger_0025new;
        4、DROP FOREIGN TABLE t_eventtrigger_0025new;
Expect      : 
    1、触发执行函数f_eventtrigger_0025()
        test_event_trigger: ddl_command_start,CREATE SERVER
        test_event_trigger: ddl_command_end,CREATE SERVER
    2、触发执行函数f_eventtrigger_0025()
        test_event_trigger: ddl_command_start,CREATE FOREIGN TABLE
        test_event_trigger: ddl_command_end,CREATE FOREIGN TABLE
    3、触发执行函数f_eventtrigger_0025()
        test_event_trigger: ddl_command_start,ALTER FOREIGN TABLE
        test_event_trigger: ddl_command_end,ALTER FOREIGN TABLE
    4、触发执行函数f_eventtrigger_0025()
        test_event_trigger: ddl_command_start,DROP FOREIGN TABLE
        test_event_trigger: sql_drop,DROP FOREIGN TABLE
        test_event_trigger: ddl_command_end,DROP FOREIGN TABLE
History     : 
"""

import sys
import os
import unittest
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH


class IndexFileDamaged(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('----------------this is setup-----------------------')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.com = Common()
        self.comsh = CommonSH('PrimaryDbBmode')
        self.userNode = self.comsh.node
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists start_eventtri;")
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists end_eventtri;")
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists drop_eventtri;")
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists rewrite_eventtri;")
        self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0025();")
        msg = self.comsh.execut_db_sql(
            f"CREATE OR REPLACE FUNCTION f_eventtrigger_0025() RETURNS event_trigger LANGUAGE plpgsql" \
            f" AS \$\$ BEGIN" \
            f" RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;" \
            f"END;" \
            f"\$\$;")
        self.logger.info(msg)
        self.assertEqual("CREATE FUNCTION", msg)
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER start_eventtri ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0025();")
        self.logger.info(msg)
        self.assertEqual("CREATE EVENT TRIGGER", msg)
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER end_eventtri ON ddl_command_end EXECUTE PROCEDURE f_eventtrigger_0025();")
        self.logger.info(msg)
        self.assertEqual("CREATE EVENT TRIGGER", msg)
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER drop_eventtri ON sql_drop EXECUTE PROCEDURE f_eventtrigger_0025();")
        self.logger.info(msg)
        self.assertEqual("CREATE EVENT TRIGGER", msg)
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER rewrite_eventtri ON table_rewrite EXECUTE PROCEDURE f_eventtrigger_0025();")
        self.logger.info(msg)
        self.assertEqual("CREATE EVENT TRIGGER", msg)
        self.comsh.execut_db_sql('DROP FOREIGN TABLE if exists t_eventtrigger_0025new;')
        self.comsh.execut_db_sql("drop SERVER if exists s_eventtrigger_0025;")

    def test_Index_file_damaged(self):
        self.logger.info('创建SERVER:CREATE SERVER s_eventtrigger_0025 FOREIGN DATA WRAPPER file_fdw;')
        msg = self.comsh.execut_db_sql('CREATE SERVER s_eventtrigger_0025 FOREIGN DATA WRAPPER file_fdw;')
        self.logger.info(msg)
        self.assertIn(f'CREATE SERVER', msg)
        self.assertIn(f'test_event_trigger: ddl_command_start CREATE SERVER', msg)
        self.assertIn(f'test_event_trigger: ddl_command_end CREATE SERVER', msg)

        self.logger.info(
            f'创建FOREIGN TABLE:CREATE FOREIGN TABLE t_eventtrigger_0025(c1 integer NULL,c2 text NULL COLLATE pg_catalog."default")' \
            f"SERVER s_eventtrigger_0025 OPTIONS (format 'text', filename '/home/$username/t_eventtrigger_0025.txt', delimiter ',');'")
        msg = self.comsh.execut_db_sql(
            f'CREATE FOREIGN TABLE t_eventtrigger_0025(c1 integer NULL,c2 text NULL COLLATE pg_catalog."default")'
            f"SERVER s_eventtrigger_0025 OPTIONS (format 'text', filename '/home/$username/t_eventtrigger_0025.txt', delimiter ',');'")
        self.logger.info(msg)
        self.assertIn(f'test_event_trigger: ddl_command_start CREATE FOREIGN TABLE', msg)
        self.assertIn(f'test_event_trigger: ddl_command_end CREATE FOREIGN TABLE', msg)
        self.assertIn("CREATE FOREIGN TABLE", msg)

        self.logger.info(
            '修改FOREIGN TABLE名称:ALTER FOREIGN TABLE t_eventtrigger_0025 RENAME TO t_eventtrigger_0025new;')
        msg = self.comsh.execut_db_sql('ALTER FOREIGN TABLE t_eventtrigger_0025 RENAME TO t_eventtrigger_0025new')
        self.logger.info(msg)
        self.assertIn(f'test_event_trigger: ddl_command_start ALTER FOREIGN TABLE', msg)
        self.assertIn(f'test_event_trigger: ddl_command_end ALTER FOREIGN TABLE', msg)
        self.assertIn("ALTER FOREIGN TABLE", msg)

        self.logger.info('删除FOREIGN TABLE:DROP FOREIGN TABLE t_eventtrigger_0025new;')
        msg = self.comsh.execut_db_sql('DROP FOREIGN TABLE t_eventtrigger_0025new;')
        self.logger.info(msg)
        self.assertIn(f'test_event_trigger: ddl_command_start DROP FOREIGN TABLE', msg)
        self.assertIn(f'test_event_trigger: sql_drop DROP FOREIGN TABLE', msg)
        self.assertIn(f'test_event_trigger: ddl_command_end DROP FOREIGN TABLE', msg)
        self.assertIn("DROP FOREIGN TABLE", msg)

    def tearDown(self):
        self.logger.info('----------------this is tearDown-----------------------')
        # 删除事件触发器
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists start_eventtri;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists end_eventtri;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists drop_eventtri;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists rewrite_eventtri;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        # 删除事件触发器函数
        msg = self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0025();")
        self.logger.info(msg)
        self.assertEqual('DROP FUNCTION', msg)
        # 删除表
        msg = self.comsh.execut_db_sql("drop FOREIGN TABLE if exists t_eventtrigger_0025;")
        self.logger.info(msg)
        self.assertIn('DROP FOREIGN TABLE', msg)
        # 删除SERVER
        msg = self.comsh.execut_db_sql("drop SERVER if exists s_eventtrigger_0025;")
        self.logger.info(msg)
        self.assertIn('DROP SERVER', msg)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
